import java.util.ArrayList;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;


public class ProfilerAgent extends Agent{
	
	ArrayList<AID> curatorAgent = new ArrayList<AID>();
	private String keyword;

	
	protected void setup(){
		System.out.println("Hallo! Profile agent "+getLocalName()+" is here.");
		
		curatorAgent.add(new AID("Seller", AID.ISLOCALNAME));
		addBehaviour(new OneShotBehaviour() {
		
			public void action() {
				// TODO Auto-generated method stub
				ACLMessage msg = new ACLMessage(ACLMessage.SUBSCRIBE);
				msg.addReceiver(curatorAgent.get(0));
				System.out.println("SHAHALABARRRA!");
				send(msg);
 
			}	
			});
		

		addBehaviour(new Bidder());
	
	}
	
	
	private class Bidder extends CyclicBehaviour {
		
		public void action() {
			//bidders informed in the beginning
			MessageTemplate msg = MessageTemplate.MatchPerformative(ACLMessage.INFORM);
			ACLMessage informmsg = receive(msg);
			
			
			//auctioneer gives price
			
			MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.CFP);
			ACLMessage cfpmsg = blockingReceive(mt);
			if (cfpmsg != null) {
				//Message received. Process it
				keyword = cfpmsg.getContent();
				try {
					if(((Thingy)cfpmsg.getContentObject()).price < 200){
						ACLMessage reply = cfpmsg.createReply();
						reply.setPerformative(ACLMessage.PROPOSE);
						
						send(reply);
						System.out.println(getLocalName()+" wants to bid " + keyword);
					}else{
						System.err.println("too expensive!!");
					}} catch (UnreadableException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				
				}
				//accept/reject
				if(cfpmsg.getPerformative() == ACLMessage.ACCEPT_PROPOSAL){
				MessageTemplate mtacc = MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL);
				ACLMessage accmsg = receive(mtacc);
				System.out.print(getLocalName()+" won ");
				}
				
				else if(cfpmsg.getPerformative() == ACLMessage.REJECT_PROPOSAL) {
				MessageTemplate mtrej = MessageTemplate.MatchPerformative(ACLMessage.REJECT_PROPOSAL);
				ACLMessage rejmsg = receive(mtrej);
				System.out.print(getLocalName()+" lost ");
				}
				
				
				//no more bidding
				else if(cfpmsg.getPerformative() == ACLMessage.INFORM) {
				MessageTemplate msgend = MessageTemplate.MatchPerformative(ACLMessage.INFORM);
				ACLMessage endmsg = blockingReceive(msgend);
				System.out.println("no more bids");
				}
		
				
			}else{
				keyword = cfpmsg.getContent();
				ACLMessage reply = cfpmsg.createReply();
				reply.setPerformative(ACLMessage.NOT_UNDERSTOOD);
				System.out.println(getLocalName()+" did not understand!");
			}
			
		}	
		}

	}







